﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Circle.Web.Application.Api;
using System.Collections;
using System.IO;
using System.Globalization;
using Newtonsoft.Json;
using Circle.Domain.Model;
using Circle.Manager;
using System.Text;
using Circle.Utility;

namespace Circle.Website.Controllers.Api
{
    public class UploadFileController : BaseApiController
    {
        private static string SavePath = "/Upload/";

        [System.Web.Http.HttpPost]
        public void Upload2()
        {
            var saveUrl = SavePath;

            //定义允许上传的文件扩展名
            var extTable = new Hashtable
                               {
                                   {"image", "gif,jpg,jpeg,png,bmp"},
                                   {"flash", "swf,flv"},
                                   {"media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"},
                                   {"file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"}
                               };

            //最大文件大小
            const int maxSize = 2000000;

            var imgFile = HttpContext.Current.Request.Files["uploadFile"];



            var dirPath = HttpContext.Current.Server.MapPath(SavePath);
            if (!Directory.Exists(dirPath))
            {
                //return ShowError("上传目录不存在。" + dirPath);
                Directory.CreateDirectory(dirPath);
            }

            var dirName = HttpContext.Current.Request.QueryString["dir"];
            if (String.IsNullOrEmpty(dirName))
            {
                dirName = "image";
            }

            if (!extTable.ContainsKey(dirName))
            {
                //return "目录名不正确。";
            }

            var fileName = imgFile.FileName;
            var extension = Path.GetExtension(fileName);
            if (extension == null)
            {
                //return "extension == null";
            }

            var fileExt = extension.ToLower();

            if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
            {
                //return "上传文件大小超过限制。";
            }

            if (String.IsNullOrEmpty(fileExt) ||
                Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                //return "上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。";
            }

            //创建文件夹
            dirPath += dirName + "/";
            saveUrl += dirName + "/";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            var ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
            dirPath += ymd + "/";
            saveUrl += ymd + "/";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
            var filePath = dirPath + newFileName;

            imgFile.SaveAs(filePath);

            var fileUrl = saveUrl + newFileName;

            Hashtable hash = new Hashtable();
            hash["error"] = 0;
            hash["url"] = fileUrl;
            HttpContext.Current.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
            HttpContext.Current.Response.Write(JsonConvert.SerializeObject(hash));
            HttpContext.Current.Response.End();

            //return Json(hash, "text/html;charset=UTF-8");
            
        }

    }
}
